﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace SCS.DAL
{
	/// <summary>
	/// 数据访问类:SCS_Receipt
	/// </summary>
	public partial class SCS_Receipt
	{
		public SCS_Receipt()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string Receipt_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from SCS_Receipt");
			strSql.Append(" where Receipt_ID='"+Receipt_ID+"' ");
			return DbHelperSQL.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(SCS.Model.SCS_Receipt model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.Receipt_ID != null)
			{
				strSql1.Append("Receipt_ID,");
				strSql2.Append("'"+model.Receipt_ID+"',");
			}
			if (model.Receipt_TimeStamp != null)
			{
				strSql1.Append("Receipt_TimeStamp,");
				strSql2.Append("'"+model.Receipt_TimeStamp+"',");
			}
			if (model.Charge_Year != null)
			{
				strSql1.Append("Charge_Year,");
				strSql2.Append("'"+model.Charge_Year+"',");
			}
			if (model.User_ID != null)
			{
				strSql1.Append("User_ID,");
				strSql2.Append("'"+model.User_ID+"',");
			}
			if (model.Student_ID != null)
			{
				strSql1.Append("Student_ID,");
				strSql2.Append("'"+model.Student_ID+"',");
			}
			if (model.Student_Fee_Payable != null)
			{
				strSql1.Append("Student_Fee_Payable,");
				strSql2.Append("'"+model.Student_Fee_Payable+"',");
			}
			if (model.Student_Paying_Type != null)
			{
				strSql1.Append("Student_Paying_Type,");
				strSql2.Append("'"+model.Student_Paying_Type+"',");
			}
			if (model.Student_Card_Pay != null)
			{
				strSql1.Append("Student_Card_Pay,");
				strSql2.Append("'"+model.Student_Card_Pay+"',");
			}
			if (model.Student_Cash_Pay != null)
			{
				strSql1.Append("Student_Cash_Pay,");
				strSql2.Append("'"+model.Student_Cash_Pay+"',");
			}
			if (model.Student_Real_Fees != null)
			{
				strSql1.Append("Student_Real_Fees,");
				strSql2.Append("'"+model.Student_Real_Fees+"',");
			}
			if (model.Student_Change != null)
			{
				strSql1.Append("Student_Change,");
				strSql2.Append("'"+model.Student_Change+"',");
			}
			if (model.Student_Payment != null)
			{
				strSql1.Append("Student_Payment,");
				strSql2.Append("'"+model.Student_Payment+"',");
			}
			if (model.Remark != null)
			{
				strSql1.Append("Remark,");
				strSql2.Append("'"+model.Remark+"',");
			}
			strSql.Append("insert into SCS_Receipt(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SCS.Model.SCS_Receipt model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update SCS_Receipt set ");
			if (model.Receipt_TimeStamp != null)
			{
				strSql.Append("Receipt_TimeStamp='"+model.Receipt_TimeStamp+"',");
			}
			else
			{
				strSql.Append("Receipt_TimeStamp= null ,");
			}
			if (model.Charge_Year != null)
			{
				strSql.Append("Charge_Year='"+model.Charge_Year+"',");
			}
			else
			{
				strSql.Append("Charge_Year= null ,");
			}
			if (model.User_ID != null)
			{
				strSql.Append("User_ID='"+model.User_ID+"',");
			}
			else
			{
				strSql.Append("User_ID= null ,");
			}
			if (model.Student_ID != null)
			{
				strSql.Append("Student_ID='"+model.Student_ID+"',");
			}
			else
			{
				strSql.Append("Student_ID= null ,");
			}
			if (model.Student_Fee_Payable != null)
			{
				strSql.Append("Student_Fee_Payable='"+model.Student_Fee_Payable+"',");
			}
			else
			{
				strSql.Append("Student_Fee_Payable= null ,");
			}
			if (model.Student_Paying_Type != null)
			{
				strSql.Append("Student_Paying_Type='"+model.Student_Paying_Type+"',");
			}
			else
			{
				strSql.Append("Student_Paying_Type= null ,");
			}
			if (model.Student_Card_Pay != null)
			{
				strSql.Append("Student_Card_Pay='"+model.Student_Card_Pay+"',");
			}
			else
			{
				strSql.Append("Student_Card_Pay= null ,");
			}
			if (model.Student_Cash_Pay != null)
			{
				strSql.Append("Student_Cash_Pay='"+model.Student_Cash_Pay+"',");
			}
			else
			{
				strSql.Append("Student_Cash_Pay= null ,");
			}
			if (model.Student_Real_Fees != null)
			{
				strSql.Append("Student_Real_Fees='"+model.Student_Real_Fees+"',");
			}
			else
			{
				strSql.Append("Student_Real_Fees= null ,");
			}
			if (model.Student_Change != null)
			{
				strSql.Append("Student_Change='"+model.Student_Change+"',");
			}
			else
			{
				strSql.Append("Student_Change= null ,");
			}
			if (model.Student_Payment != null)
			{
				strSql.Append("Student_Payment='"+model.Student_Payment+"',");
			}
			else
			{
				strSql.Append("Student_Payment= null ,");
			}
			if (model.Remark != null)
			{
				strSql.Append("Remark='"+model.Remark+"',");
			}
			else
			{
				strSql.Append("Remark= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where Receipt_ID='"+ model.Receipt_ID+"' ");
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string Receipt_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SCS_Receipt ");
			strSql.Append(" where Receipt_ID='"+Receipt_ID+"' " );
			int rowsAffected=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Receipt_IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SCS_Receipt ");
			strSql.Append(" where Receipt_ID in ("+Receipt_IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SCS.Model.SCS_Receipt GetModel(string Receipt_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" Receipt_ID,Receipt_TimeStamp,Charge_Year,User_ID,Student_ID,Student_Fee_Payable,Student_Paying_Type,Student_Card_Pay,Student_Cash_Pay,Student_Real_Fees,Student_Change,Student_Payment,Remark ");
			strSql.Append(" from SCS_Receipt ");
			strSql.Append(" where Receipt_ID='"+Receipt_ID+"' " );
			SCS.Model.SCS_Receipt model=new SCS.Model.SCS_Receipt();
			DataSet ds=DbHelperSQL.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SCS.Model.SCS_Receipt DataRowToModel(DataRow row)
		{
			SCS.Model.SCS_Receipt model=new SCS.Model.SCS_Receipt();
			if (row != null)
			{
				if(row["Receipt_ID"]!=null)
				{
					model.Receipt_ID=row["Receipt_ID"].ToString();
				}
				if(row["Receipt_TimeStamp"]!=null && row["Receipt_TimeStamp"].ToString()!="")
				{
					model.Receipt_TimeStamp=DateTime.Parse(row["Receipt_TimeStamp"].ToString());
				}
				if(row["Charge_Year"]!=null)
				{
					model.Charge_Year=row["Charge_Year"].ToString();
				}
				if(row["User_ID"]!=null)
				{
					model.User_ID=row["User_ID"].ToString();
				}
				if(row["Student_ID"]!=null)
				{
					model.Student_ID=row["Student_ID"].ToString();
				}
				if(row["Student_Fee_Payable"]!=null)
				{
					model.Student_Fee_Payable=row["Student_Fee_Payable"].ToString();
				}
				if(row["Student_Paying_Type"]!=null)
				{
					model.Student_Paying_Type=row["Student_Paying_Type"].ToString();
				}
				if(row["Student_Card_Pay"]!=null)
				{
					model.Student_Card_Pay=row["Student_Card_Pay"].ToString();
				}
				if(row["Student_Cash_Pay"]!=null)
				{
					model.Student_Cash_Pay=row["Student_Cash_Pay"].ToString();
				}
				if(row["Student_Real_Fees"]!=null)
				{
					model.Student_Real_Fees=row["Student_Real_Fees"].ToString();
				}
				if(row["Student_Change"]!=null)
				{
					model.Student_Change=row["Student_Change"].ToString();
				}
				if(row["Student_Payment"]!=null)
				{
					model.Student_Payment=row["Student_Payment"].ToString();
				}
				if(row["Remark"]!=null)
				{
					model.Remark=row["Remark"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Receipt_ID,Receipt_TimeStamp,Charge_Year,User_ID,Student_ID,Student_Fee_Payable,Student_Paying_Type,Student_Card_Pay,Student_Cash_Pay,Student_Real_Fees,Student_Change,Student_Payment,Remark ");
			strSql.Append(" FROM SCS_Receipt ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Receipt_ID,Receipt_TimeStamp,Charge_Year,User_ID,Student_ID,Student_Fee_Payable,Student_Paying_Type,Student_Card_Pay,Student_Cash_Pay,Student_Real_Fees,Student_Change,Student_Payment,Remark ");
			strSql.Append(" FROM SCS_Receipt ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM SCS_Receipt ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Receipt_ID desc");
			}
			strSql.Append(")AS Row, T.*  from SCS_Receipt T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
		#region  MethodEx

		#endregion  MethodEx
	}
}

